/**
 * $Id$
 * @author Skautron
 * @date   06/09/2011 20:09:02
 *
 * Copyright (C) 2011 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.ui.context;

import org.sk.gsync.app.GoogleConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.google.gdata.util.AuthenticationException;

public class LoginDialogContext extends AbstractContext {

    @Autowired
    @Qualifier("googleConnection")
    private GoogleConnection googleConnection;

    private boolean _auth = false;

    private static final int CHANCES = 3;

    private int _tries = 0;

    public void login() throws AuthenticationException {
        String user = (String) getValue(LoginFields.GMAIL_EMAIL);
        String password = (String) getValue(LoginFields.PASSWORD);
        String token = (String) getValue(LoginFields.TOKEN);
        googleConnection.connect(user, password, token);
        _auth = true;
        _tries++;
    }

    public boolean isAuth() {
        return _auth;
    }

    public boolean isTriesExausted() {
        return (_tries == CHANCES);
    }

    /**
     * @see org.sk.gsync.ui.context.AbstractContext#load()
     */
    @Override
    public void load() {
    }

    /**
     * @see org.sk.gsync.ui.context.AbstractContext#save()
     */
    @Override
    public void save() {
    }

}
